package com.tbit.uqbike.client.remote.tenxun;


import com.alibaba.fastjson.JSON;
import com.tbit.uqbike.client.remote.tenxun.common.Credential;
import com.tbit.uqbike.client.remote.tenxun.common.exception.TencentCloudSDKException;
import com.tbit.uqbike.client.remote.tenxun.common.profile.ClientProfile;
import com.tbit.uqbike.client.remote.tenxun.common.profile.HttpProfile;
import com.tbit.uqbike.client.remote.tenxun.models.*;
import com.tbit.uqbike.client.util.LogUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @Author: chen
 * @Description: e证通实名认证工具类
 * @Date: 创建于 17:05 2021/9/10
 */
@Slf4j
public class GetEidToken {

    /**
     * 后台传递实名信息
     */
    public static final String FACE_TYPE_4 = "4";

    /**
     * 获取token
     *
     * @param secretId   id
     * @param secretKey  秘钥
     * @param MerchantId 商户id
     * @param name       姓名
     * @param idCard     身份证号码
     * @return
     */
    public static String getToken(String secretId, String secretKey, String MerchantId, String name, String idCard) {

        try {
            // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
            // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
            FaceidClient client = getClient(secretId, secretKey);
            // 实例化一个请求对象,每个接口都会对应一个request对象
            GetEidTokenRequest req = new GetEidTokenRequest();
            req.setMerchantId(MerchantId);
            GetEidTokenConfig getEidTokenConfig1 = new GetEidTokenConfig();
            getEidTokenConfig1.setInputType(FACE_TYPE_4);
            req.setConfig(getEidTokenConfig1);
            req.setIdCard(idCard);
            req.setName(name);
            // 返回的resp是一个GetEidTokenResponse的实例，与请求对象对应
            GetEidTokenResponse resp = client.GetEidToken(req);
            // 输出json格式的字符串回包
            LogUtil.info("人脸传入=" + JSON.toJSONString(req) + "人脸返回=" + GetEidTokenResponse.toJsonString(resp));
            return resp.getEidToken();
        } catch (TencentCloudSDKException e) {
            LogUtil.error(name + "," + idCard + "获取Etoken失败" + e.toString());
        }
        return null;
    }

    /**
     * 获取bizToken
     *
     * @param secretId  secretId
     * @param secretKey secretKey
     * @param ruleId    ruleId
     * @param name
     * @param idCard
     * @return
     */
    public static DetectAuthResponse getBizToken(String secretId, String secretKey, String ruleId, String name, String idCard, String
            redirectUrl, String imageBase64) {
        try {
            DetectAuthRequest req = new DetectAuthRequest();
            FaceidClient client = getClient(secretId, secretKey);
            req.setRuleId(ruleId);
            req.setIdCard(idCard);
            req.setName(name);
            req.setRedirectUrl(redirectUrl);
            req.setImageBase64(imageBase64);
            DetectAuthResponse detectAuthResponse = client.DetectAuth(req);
            return detectAuthResponse;
        } catch (TencentCloudSDKException e) {
            log.error("人脸识别异常", e);
        }
        return null;
    }

    /**
     * 获取人脸识别结果
     *
     * @param secretId
     * @param secretKey
     * @param ruleId
     * @param bizToken
     * @return
     */
    public static String getDetectInfo(String secretId, String secretKey, String ruleId, String bizToken) {
        try {
            GetDetectInfoRequest req = new GetDetectInfoRequest();
            FaceidClient client = getClient(secretId, secretKey);
            req.setRuleId(ruleId);
            req.setBizToken(bizToken);
            /**指定拉取的结果信息，取值（0：全部；1：文本类；2：身份证信息；3：视频最佳截图信息）。
             如 13表示拉取文本类、视频最佳截图信息。*/
            req.setInfoType("13");
            GetDetectInfoResponse getDetectInfoResponse = client.GetDetectInfo(req);
            return getDetectInfoResponse.getDetectInfo();
        } catch (TencentCloudSDKException e) {
            log.error("获取人脸结果异常" + bizToken, e);
        }
        return null;
    }


    /**
     * 获取请求产品的client对象
     *
     * @param secretId
     * @param secretKey
     * @return
     */
    public static FaceidClient getClient(String secretId, String secretKey) {
        Credential cred = new Credential(secretId, secretKey);
        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("faceid.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);

        // 实例化要请求产品的client对象,clientProfile是可选的
        FaceidClient client = new FaceidClient(cred, "", clientProfile);
        return client;
    }


    public static void main(String[] args) {
        getToken("AKIDQjyAE5h4c8rT5c1HPPMI9uY8c5SkzEMO", "0ENQMD5sX8d1JUaaUHzgJpFZnttocJsL", "0NSJ2109021116533979", "钟恒基", "360781199701220030");
    /*try{
      // 实例化一个认证对象，入参需要传入腾讯云账户secretId，secretKey,此处还需注意密钥对的保密
      // 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
      Credential cred = new Credential("AKIDQjyAE5h4c8rT5c1HPPMI9uY8c5SkzEMO", "0ENQMD5sX8d1JUaaUHzgJpFZnttocJsL");
      // 实例化一个http选项，可选的，没有特殊需求可以跳过
      HttpProfile httpProfile = new HttpProfile();
      httpProfile.setEndpoint("faceid.tencentcloudapi.com");
      // 实例化一个client选项，可选的，没有特殊需求可以跳过
      ClientProfile clientProfile = new ClientProfile();
      clientProfile.setHttpProfile(httpProfile);

      // 实例化要请求产品的client对象,clientProfile是可选的
      FaceidClient client = new FaceidClient(cred, "", clientProfile);
      // 实例化一个请求对象,每个接口都会对应一个request对象
      GetEidTokenRequest req = new GetEidTokenRequest();
      req.setMerchantId("0NSJ2109021116533979");
      GetEidTokenConfig getEidTokenConfig1 = new GetEidTokenConfig();
      getEidTokenConfig1.setInputType("4");
      req.setConfig(getEidTokenConfig1);
      req.setIdCard("360781199701220030");
      req.setName("钟恒基");
      // 返回的resp是一个GetEidTokenResponse的实例，与请求对象对应
      GetEidTokenResponse resp = client.GetEidToken(req);
      // 输出json格式的字符串回包
      System.out.println(GetEidTokenResponse.toJsonString(resp));
    } catch (TencentCloudSDKException e) {
      System.out.println(e.toString());
    }*/
    }
}
